//===========================================
// Function : Asynchronous read write RAM 
// Coder    : Deepak Kumar Tala
// Date     : 1-Nov-2005
//===========================================
module ram_sp_ar_aw #(parameter DATA_WIDTH = 8,
                  parameter ADDR_WIDTH = 8,
                  parameter RAM_DEPTH = (1 << ADDR_WIDTH))(
input  wire [ADDR_WIDTH-1:0] address  , // Address Input
inout  wire [DATA_WIDTH-1:0] data     , // Data bi-directional
input  wire                  cs       , // Chip Select
input  wire                  we       , // Write Enable/Read Enable
input  wire                  oe         // Output Enable
);          
//--------------Internal variables---------------- 
reg [DATA_WIDTH-1:0]   data_out ;
// Use Associative array to save memory footprint
typedef reg [ADDR_WIDTH-1:0] mem_addr;
reg [DATA_WIDTH-1:0] mem [mem_addr];

//--------------Code Starts Here------------------ 
// Tri-State Buffer control 
// output : When we = 0, oe = 1, cs = 1
assign data = (cs && oe && !we) ? data_out : 8'bz; 

// Memory Write Block 
// Write Operation : When we = 1, cs = 1
always_latch
begin : MEM_WRITE
  if ( cs && we ) begin
    mem[address] = data;
  end
end

// Memory Read Block 
// Read Operation : When we = 0, oe = 1, cs = 1
always_latch
begin : MEM_READ
  if (cs && !we && oe)  begin
     data_out = mem[address];
  end
end

endmodule // End of Module ram_sp_ar_aw
